﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace quanlycd3lop.Data
{
    class AccessData
    {
        private SqlConnection con = new SqlConnection();
        public SqlConnection Getconn
        {
            get
            {
                return con;
            }
        }
        public AccessData()
        {
            con.ConnectionString ="Data Source=PC-PC\\SQLEXPRESS;Initial Catalog=quanlycd;Integrated Security=True";
        }
        public void Open()
            {
                if(con.State == ConnectionState.Closed)
                    con.Open();
            }
        public void Close()
        {
            if(con.State == ConnectionState.Open)
                con.Close();
        }
        public DataTable Get_DataTable(String sql)
        {
            SqlDataAdapter adp=new SqlDataAdapter(sql,con);
            DataTable tb=new DataTable();
            adp.Fill(tb);
            return tb;
        }
        public DataSet Get_DataSet()
        {
            DataTable tb=new DataTable();
            DataSet ds= new DataSet();
            ds.Tables.Add(tb);
            return ds;
        }
        public void Excute_SQL(String sql)
        {
            SqlCommand cmd= new SqlCommand();
            cmd.Connection=con;
            cmd.CommandText=sql;
            cmd.CommandType=CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        public bool login(string name, string pass)
        {
            SqlCommand cmd = new SqlCommand("select * from TAIKHOAN where TenDN ='"+name+"' and Matkhau='" +pass+"'",Getconn);
            Getconn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            if(rd.Read())
            {
                Getconn.Close();
                return true;
            }
            Getconn.Close();
            return false;
        }
        public DataTable Excute_ps(string name_ps,SqlParameter[] pr)
        {
            SqlCommand cmd=new SqlCommand();
            cmd.CommandText=name_ps;
            cmd.CommandType=System.Data.CommandType.StoredProcedure;
            cmd.Connection=con;
            cmd.Parameters.AddRange(pr);
            SqlDataAdapter adp=new SqlDataAdapter(cmd);
            DataTable tb=new DataTable();
            adp.Fill(tb);
            return tb;

        }
        public void Update_DataSet(ref DataTable tb, String SQL)
        {
            SqlDataAdapter adp = new SqlDataAdapter(SQL, con);
            SqlCommandBuilder bd = new SqlCommandBuilder();
            bd.DataAdapter = adp;
            adp.Update(tb);
        }
    }
}
